<?php

use Welcomescreen\Agavi\Model;
use Welcomescreen\Objects;

class VisitManagerModel extends Model\BaseModel implements \AgaviISingletonModel {
	/**
	 * @var bool
	 */
	protected $initialized = false;
	
	/**
	 * @var PDO
	 */
	protected $pdo;
	
	/**
	 * @param AgaviContext $context
	 * @param array $parameters
	 */
	public function initialize(AgaviContext $context, array $parameters = array()) {
		parent::initialize($context, $parameters);
		if(!$this->initialized) {
			$this->pdo = $context->getDatabaseConnection();
			$this->initialized = true;
		}
	}
	
	public function hasVisit($id) {
		$sql = 'SELECT id FROM visit WHERE id = ? LIMIT 1';
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute(array($id));
		if($data = $stmt->fetch()) {
			return true;
		}
		return false;
	}
	
	public function getVisit($id) {
		$userManager = $this->getContext()->getModel('UserManager');
		$sql = 'SELECT id, revision, manualActivationOverride, startDate, endDate, lastEditUser, visitorName, templateName, templateData FROM visit WHERE id = ?';
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute(array($id));
		
		$objects = array();
		while($data = $stmt->fetch()) {
			$data['templateData'] = json_decode($data['templateData'], true);
			$objects[] = Objects\Visit::initFromArray($data, $userManager);
		}
		if(count($objects) == 0) {
			throw new Exception\NonexistentObjectException('The visit with the id "' . $id . '" does not exist');
		}
		return $objects[0];
	}
	
	public function getVisits() {
		$userManager = $this->getContext()->getModel('UserManager');
		$sql = 'SELECT id, revision, manualActivationOverride, startDate, endDate, lastEditUser, visitorName, templateName, templateData FROM visit';
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute();
		
		$objects = array();
		while($data = $stmt->fetch()) {
			$data['templateData'] = json_decode($data['templateData'], true);
			$objects[] = Objects\Visit::initFromArray($data, $userManager);
		}
		return $objects;
	}
	
	public function save(Objects\Visit $visit) {
		if(!$visit->getId() || $this->hasVisit($visit->getId())) {
			$sql = 'INSERT INTO visit (id, revision, manualActivationOverride, startDate, endDate, lastEditUser, visitorName, templateName, templateData) VALUES (:id, :revision, :manualActivationOverride, :startDate, :endDate, :lastEditUser, :visitorName, :templateName, :templateData)';
			// i hate pdo for complaining about entries in the array which are not defined as bound parameter ... 
			// so let's unset all keys which it doesn't know about
			$visitArray = $visit->toArray();
			unset($visitArray['active']);
			$visitArray['templateData'] = json_encode($visitArray['templateData']);
			$stmt = $this->pdo->prepare($sql);
			$stmt->execute($visitArray);
			// update the object with the id from the database
			$visit->setId($this->pdo->lastInsertId());
		}
	}
	
	public function delete(Objects\Visit $visit) {
		if(!$visit->getId()) {
			throw new \InvalidArgumentException('You can\'t delete an unsaved visit');
		}
		
		$sql = 'DELETE FROM visit WHERE id = ?';
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute(array($visit->getId()));
	}
}

?>